package com.taobaolite.cart_has_items;

import cn.dev33.satoken.annotation.SaCheckLogin;
import com.taobaolite.item.ItemService;
import com.taobaolite.user.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

//@SaCheckLogin
@RestController
@RequestMapping("/api/v1/cart")
public class CartController {
    private final CartService cartService;
    private final ItemService itemService;

    @Autowired
    public CartController(CartService cartService, ItemService itemService) {
        this.cartService = cartService;
        this.itemService = itemService;
    }

    @GetMapping
    public List<CartBody> getCart(@RequestParam("userId") Long userId) {
        User.checkToken();
        return cartService.getCart(userId);
    }

    @PutMapping
    public PutCart putCart(
            @RequestParam("userId") Long userId,
            @RequestParam("itemId") Long itemId,
            @RequestParam("amount") Integer amount
    ) {
        User.checkToken();
        return cartService.putCart(userId, itemId, amount);
    }

    @PostMapping
    public CartBody updateCart(
            @RequestParam("userId") Long userId,
            @RequestBody PutCart putCart) {
        User.checkToken();
        return cartService.updateCart(userId, putCart);
    }

    @DeleteMapping
    public void deleteItemInCart(
            @RequestParam("userId") Long userId,
            @RequestParam("itemId") Long itemId) {
        User.checkToken();
        cartService.deleteItemInCart(userId, itemId);
    }
}

